# read file

library(xts)
library(psych)  # geometric.mean
source('bhp.R')

read <- function(filename)
{
	X <- read.table(paste(paste('data/',filename,sep=''),'csv',sep='.'), sep=',')
	xts(X[,4], as.Date(paste(X[,1], X[,2], X[,3], sep='-')))
}

Xstate <- function(X, state)
{
	mem <- nchar(state)

	# relative differences
	ts <- as.numeric(X)
	ts <- diff(ts)/(ts[-length(ts)])

	# embedding
	e <- embed(ts, mem)
	codify <- function(x) sum((x>=0) * 2^(0:(mem-1)))
	codes <- apply(e, 1, codify)
	e[codes==strtoi(state,2),1]
}

getsymbol <- function(n, mem)
	paste(c('-','+')[(trunc(n/(2^((mem-1):0))) %% 2)+1], collapse='')

# non-linear transformation .. power the data vector to lambda
# http://en.wikipedia.org/wiki/Power_transform

power_transform <- function(X, lambda)
{
	X <- abs(X)
	X <- X[X > 1e-6]  # geometric.mean dont work well with zeros
	GM <- geometric.mean(X)
	Y <- if(lambda == 0) GM*log(X) else (X^lambda - 1) / (lambda*GM)
	(Y - mean(Y)) / sd(Y)
}


# produces a Markov-like matrix
# it is a collapsed version where the 
#
# to produce a latex table, use:
# xtable(memmarkov("goog", 1, FALSE), "memory 1")

memmarkov <- function(X, mem, relfreq)
{
	stopifnot(mem >= 0)

	# relative differences
	ts <- as.numeric(X)
	ts <- diff(ts)/(ts[-length(ts)])

	# embedding
	e <- embed(ts, mem+1)

	# codify: binary (00 down/down, 01 down/up, ...)
	codify <- function(x) sum((x>=0) * 2^(0:mem))

	codes <- apply(e, 1, codify)
	Freqs <- table(codes%%2, trunc(codes/2))  # (jump, current state)

	# pretty-fy column and row names
	if(mem == 0)
		colnames(Freqs) <- ''
	else
		colnames(Freqs) <- as.character(lapply(0:(2^mem-1), function(x) getsymbol(x,mem)))
	rownames(Freqs) <- c('-','+')

	if(relfreq) {
		div <- apply(Freqs, 2, sum)
		Markov <- Freqs / rbind(div,div)  # relative freqs
	}
	else
		Markov <- Freqs
	Markov
}

